#!/bin/bash

pwd_dir=$(dirname $0) #获取当前脚本所在目录
source $pwd_dir/../config.mk #source执行指定命令，加载父目录下的 config.mk 文件，让其中定义的变量和函数在当前脚本中生效

get_config_info() #定义名为 get_config_info 的函数，用于获取并设置系统配置信息
{
	FLASH_TYPE=$CONFIG_FLASH_TYPE
	SCREEN_TYPE=$SCREEN_TYPE
	WIFI_TYPE=$CONFIG_WIFI_TYPE
	CHIP_SERIES=$CONFIG_CHIP_SERIES

    rootfs_dir=$CONFIG_TOP_DIR/rootfs
    driver_install_dir=$CONFIG_TOP_DIR/os/driver
    image_install_dir=$CONFIG_IMAGE_OUTPUT_DIR
    sample_install_dir=$rootfs_dir/samples

    echo "FLASH_TYPE=$FLASH_TYPE, WIFI_TYPE=$WIFI_TYPE, CHIP_SERIES=$CHIP_SERIES"
}

extract_fs()
{
    # 解压缩根文件系统
#    if [ -d "resource" ]; then
#        echo "delete the resource folder !"
#        rm "resource" -rf
#    fi


    if 	test $CHIP_SERIES = "AK3918AV200" ; then
        if [ -d "resource" ]; then
            echo "delete the resource folder !"
            rm "resource" -rf
        fi
        rm -rf rootfs.tar.gz 
        if test "$CONFIG_CROSS_COMPILE" = "riscv64-anyka-linux-musl-" ; then
            echo "musl rootfs!"
            tar zxvf rootfs_av200_musl.tar.gz  -C $rootfs_dir
            ln -s busybox_av200_musl.tar.gz rootfs.tar.gz
        elif test "$CONFIG_CROSS_COMPILE" = "riscv64-anyka-linux-gnu-"; then
            echo "glibc rootfs!"
            tar zxvf rootfs_av200_glibc.tar.gz -C $rootfs_dir
            ln -s busybox_av200_glibc.tar.gz rootfs.tar.gz
        else
            echo "The toochain $CONFIG_CROSS_COMPILE did not match the file system!"
            exit 1
        fi
    else #非 AK3918AV200 芯片处理
        echo "extract basic resource"
 #       tar zxvf resouce.tar.gz -C $rootfs_dir
    fi

    ./extract.sh
}

process_wifi_driver()
{
    rm -rf rootfs/usr/modules/rtl*
    rm -rf rootfs/usr/modules/atbm*
    rm -rf rootfs/usr/modules/ssv*
    rm -rf rootfs/usr/modules/cfg*
    rm -rf rootfs/usr/modules/ATBM*
    # 根据选择的WIFI类型，清理掉无用的驱动

    if test $WIFI_TYPE = "NO_WIFI" ; then
		true
    elif test $WIFI_TYPE = "rtl8188ftv" ; then
      if 	test $CHIP_SERIES = "AK3918AV200" ; then
        cp -rf $driver_install_dir/external/rtl8188fu.ko rootfs/usr/modules/
      else
        cp -rf $driver_install_dir/external/rtl8188ftv.ko rootfs/usr/modules/
      fi
    elif test $WIFI_TYPE = "rtl8189ftv" ; then
        cp -rf $driver_install_dir/external/rtl8189ftv.ko rootfs/usr/modules/
    elif test $WIFI_TYPE = "atbm6031" ; then
		cp -rf $driver_install_dir/external/atbm6031.ko rootfs/usr/modules/
  elif test $WIFI_TYPE = "atbm6032" ; then
		cp -rf $driver_install_dir/external/atbm6032.ko rootfs/usr/modules/
  elif test $WIFI_TYPE = "atbm6012b" ; then
		cp -rf $driver_install_dir/external/atbm6032.ko rootfs/usr/modules/
  elif test $WIFI_TYPE = "atbm6132" ; then
    cp -rf $driver_install_dir/external/atbm6132_wifi_usb.ko rootfs/usr/modules/
	elif test $WIFI_TYPE = "atbm6031x" ; then
		cp -rf $driver_install_dir/external/atbm6031x.ko rootfs/usr/modules/
	elif test $WIFI_TYPE = "atbm6012bx" ; then
		cp -rf $driver_install_dir/external/atbm6012bx.ko rootfs/usr/modules/
	elif test $WIFI_TYPE = "atbm6062u" ; then
		cp -rf $driver_install_dir/external/ATBM606x_wifi_usb.ko rootfs/usr/modules/
		cp -rf $driver_install_dir/external/cfg80211.ko rootfs/usr/modules/
	elif test $WIFI_TYPE = "atbm6062s" ; then
		cp -rf $driver_install_dir/external/ATBM606x_wifi_sdio.ko rootfs/usr/modules/
		cp -rf $driver_install_dir/external/cfg80211.ko rootfs/usr/modules/
    elif test $WIFI_TYPE = "ssv6x5x" ; then
        cp -rf $driver_install_dir/external/ssv6x5x.ko rootfs/usr/modules/
    fi #按照wifi型号选择驱动放到usr/module里面
}

process_screen_driver() #该函数的功能很可能是处理屏幕驱动相关的操作，比如安装、清理屏幕驱动等
{
    rm -rf rootfs/usr/modules/ts_*  #rm,remove是删除文件或目录的命令
    #这一步的作用是清理已有的屏幕相关的触摸驱动文件，为后续安装合适的驱动做准备


    if test -z $SCREEN_TYPE ; then
        return
    fi

    if test $SCREEN_TYPE = "NO_SCREEN" ; then
        true
    elif test $SCREEN_TYPE = "RGB" ; then
        cp -rf $driver_install_dir/external/ts_icn85xx.ko rootfs/usr/modules/
        true
    elif test $SCREEN_TYPE = "MIPI" ; then
        cp -rf $driver_install_dir/external/ts_icn85xx.ko rootfs/usr/modules/
        true
    else
        echo "Screen Type Error, please check!"
        exit 1
    fi
}

process_sensor_driver() #获取对应传感器的驱动
{
    rm -rf rootfs/usr/modules/sensor_*  #清除所有传感器驱动

    #cp -rf ko/external/sensor_*.ko rootfs/usr/modules/
    if test $CHIP_SERIES = "AK37E" ; then
    	cp -rf $driver_install_dir/external/sensor_gc0308.ko rootfs/usr/modules/
    	cp -rf $driver_install_dir/external/sensor_tp9950.ko rootfs/usr/modules/
    elif 	test $CHIP_SERIES = "AK37D" ; then
        cp -rf $driver_install_dir/external/sensor_ar0230.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_pr2000.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_ps5268.ko rootfs/usr/modules/
    elif 	test $CHIP_SERIES = "AK39EV33X" ; then
        cp -rf $driver_install_dir/external/sensor_f22_f23_f28_f35_f37.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc3335.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_gc4653.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_gc2063.ko rootfs/usr/modules/
    elif 	test $CHIP_SERIES = "AK3918AV100" ; then
        cp -rf $driver_install_dir/external/sensor_sc3336.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_gc4653.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_gc2063.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc2336.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc2336x.ko rootfs/usr/modules/
		cp -rf $driver_install_dir/external/sensor_sc1346.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_mis2008.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc223a.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc401ai.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_gc3003.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc5238.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/ak_tsensor.ko rootfs/usr/modules/
    elif 	test $CHIP_SERIES = "AK3918EV300L" ; then
        cp -rf $driver_install_dir/external/sensor_gc2063.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_gc2083.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc2336.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc223a.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_sc3336.ko rootfs/usr/modules/
        cp -rf $driver_install_dir/external/sensor_gc3003.ko rootfs/usr/modules/
		cp -rf $driver_install_dir/external/sensor_sc1346.ko rootfs/usr/modules/
		cp -rf $driver_install_dir/external/sensor_mis2006.ko rootfs/usr/modules/
    elif 	test $CHIP_SERIES = "AK3918AV200" ; then
        cp $driver_install_dir/external/sensor_gc2063.ko rootfs/usr/modules/
        cp $driver_install_dir/external/sensor_gc4653.ko rootfs/usr/modules/
        cp $driver_install_dir/external/sensor_gc5603.ko rootfs/usr/modules/
        cp $driver_install_dir/external/sensor_os04a10.ko rootfs/usr/modules/
        cp $driver_install_dir/external/sensor_os08b10.ko rootfs/usr/modules/
        cp $driver_install_dir/external/sensor_os08a20.ko rootfs/usr/modules/
    else
    	exit 1
    fi
}

# 安装驱动到文件系统
install_driver()
{
    # 删除文件统中遗留的文件
	if test -d rootfs/usr/modules ; then
        rm -rf rootfs/usr/modules/*
        rm -rf rootfs/lib/modules/*
    else
		mkdir rootfs/usr/modules
	fi

    #拷贝driver ko
    cp -rf $driver_install_dir/external/*.ko rootfs/usr/modules

    #拷贝kernel ko
    cp -rf $driver_install_dir/internal/lib rootfs/
    rm -rf rootfs/lib/modules/4.4.*/kernel/crypto/
    rm -rf rootfs/lib/modules/4.4.*/kernel/drivers/staging/rtl8188eu
    rm -rf rootfs/lib/modules/4.4.*/kernel/net/wireless/mac80211.ko
    rm -rf rootfs/lib/modules/4.4.*/kernel/net/bridge/*.ko
    rm -rf rootfs/lib/modules/4.4.*/kernel/net/llc/llc.ko
    rm -rf rootfs/lib/modules/4.4.*/kernel/net/802/stp.ko

    process_wifi_driver

    process_screen_driver

    process_sensor_driver
    #process_wifi_driver、process_screen_driver 和 process_sensor_driver 这三个函数，
    #分别对 WiFi 驱动、屏幕驱动和传感器驱动进行进一步的处理，比如根据配置选择合适的驱动并安装等
}

# 安装平台中间件的库文件到文件系统
install_platform()
{
    cp -rf $CONFIG_TOP_DIR/platform/lib/*.so rootfs/usr/lib
}

process_flash_scripts()#根据Flash类型选择不同的启动脚本和更新工具
{
    if test $FLASH_TYPE = "NOR" ; then
        cp ./scripts/flash/rc.local.nor rootfs/etc/init.d/rc.local
        cp ./scripts/flash/update.nor.sh rootfs/sbin/update.sh
    elif test $FLASH_TYPE = "NAND" ; then
        cp ./scripts/flash/rc.local.nand rootfs/etc/init.d/rc.local #系统启动脚本
        cp ./scripts/flash/update.nand.sh rootfs/sbin/update.sh #NAND Flash 更新脚本
		cp ./scripts/flash/nand_update rootfs/bin/nand_update #NAND Flash 专用更新工具
    else
        echo "Flash type error, please check!"
        exit 1
    fi
    
    cp ./scripts/flash/update_ddr.sh rootfs/sbin/update_ddr.sh #DDR 内存更新脚本
}

process_sensor_scripts() #这些脚本通常用于传感器的初始化、配置或测试等操作
{
    cp scripts/sensor/*.sh rootfs/usr/sbin/
}

process_screen_scripts()
{
    if test -z $SCREEN_TYPE ; then
        return
    fi

    if test $SCREEN_TYPE = "NO_SCREEN" ; then
        true
    elif test $SCREEN_TYPE = "RGB" ; then
        sed -i 's/ts_icn86xx/ts_icn85xx/' scripts/screen/screen.sh
        cp scripts/screen/screen.sh rootfs/usr/sbin/
    elif test $SCREEN_TYPE = "MIPI" ; then
        sed -i 's/ts_icn86xx/ts_icn85xx/' scripts/screen/screen.sh
        cp scripts/screen/screen.sh rootfs/usr/sbin/
    else
        echo "Screen Type Error, please check!"
        exit 1
    fi
}

process_lcd_scripts() # LCD（液晶显示器）相关的脚本和驱动
{
    cp $driver_install_dir/external/ak_lcd.ko rootfs/usr/modules/
    cp scripts/screen/lcd.sh rootfs/usr/sbin/
}

process_wifi_scripts()
{
    # 根据选择的WIFI类型，清理掉无用的驱动
    if test $WIFI_TYPE = "NO_WIFI" ; then
        true
    elif test $WIFI_TYPE = "rtl8188ftv" ; then
        cp scripts/wifi/rtl8188.sh rootfs/usr/sbin/wifi_driver.sh
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
    elif test $WIFI_TYPE = "rtl8189ftv" ; then
        cp scripts/wifi/rtl8189.sh rootfs/usr/sbin/wifi_driver.sh
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
    elif test $WIFI_TYPE = "atbm6031" ; then
        cp scripts/wifi/atbm6031.sh rootfs/usr/sbin/wifi_driver.sh
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
    elif test $WIFI_TYPE = "atbm6032" ; then
        cp scripts/wifi/atbm6032.sh rootfs/usr/sbin/wifi_driver.sh
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
    elif test $WIFI_TYPE = "atbm6012b" ; then
        cp scripts/wifi/atbm6032.sh rootfs/usr/sbin/wifi_driver.sh
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
    elif test $WIFI_TYPE = "atbm6132" ; then
        cp scripts/wifi/atbm6132.sh rootfs/usr/sbin/wifi_driver.sh
        cp scripts/wifi/wifi_bt_driver_6132.sh rootfs/usr/sbin/
        cp scripts/wifi/connect_ap.sh rootfs/usr/sbin/
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli
        rm rootfs/usr/sbin/atbm_tool
        rm rootfs/usr/sbin/nimble_linux
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
        mv rootfs/usr/sbin/atbm_cli_6132 rootfs/usr/sbin/atbm_cli
        mv rootfs/usr/sbin/atbm_tool_6132 rootfs/usr/sbin/atbm_tool
        mv rootfs/usr/sbin/nimble_linux_6132 rootfs/usr/sbin/nimble_linux
    elif test $WIFI_TYPE = "atbm6031x" ; then
        cp scripts/wifi/atbm6031x.sh rootfs/usr/sbin/wifi_driver.sh
        cp scripts/wifi/wifi_bt_driver_6031x.sh rootfs/usr/sbin/
        cp scripts/wifi/connect_ap.sh rootfs/usr/sbin/
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        if 	test $CONFIG_CHIP_TYPE = "AK3918EV300W" ; then
            echo "chip type is AK3918EV300W!"
            rm rootfs/usr/sbin/atbm_cli
            rm rootfs/usr/sbin/atbm_tool
            rm rootfs/usr/sbin/nimble_linux
            mv rootfs/usr/sbin/atbm_cli_6031x_300w rootfs/usr/sbin/atbm_cli
            mv rootfs/usr/sbin/atbm_tool_6031x_300w rootfs/usr/sbin/atbm_tool
            mv rootfs/usr/sbin/nimble_linux_6031x_300w rootfs/usr/sbin/nimble_linux
        else
        	  echo "chip type is not AK3918EV300W!"
            rm rootfs/usr/sbin/atbm_cli_6031x_300w
            rm rootfs/usr/sbin/atbm_tool_6031x_300w
            rm rootfs/usr/sbin/nimble_linux_6031x_300w
        fi
    elif test $WIFI_TYPE = "atbm6012bx" ; then
        cp scripts/wifi/atbm6012bx.sh rootfs/usr/sbin/wifi_driver.sh
        cp scripts/wifi/wifi_bt_driver_6012bx.sh rootfs/usr/sbin/
        cp scripts/wifi/connect_ap.sh rootfs/usr/sbin/
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
    elif test $WIFI_TYPE = "atbm6062u" ; then
        cp scripts/wifi/atbm6062u.sh rootfs/usr/sbin/wifi_driver.sh
        cp scripts/wifi/wifi_bt_driver_6062u.sh rootfs/usr/sbin/
        cp scripts/wifi/connect_ap.sh rootfs/usr/sbin/
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli
        rm rootfs/usr/sbin/atbm_tool
        rm rootfs/usr/sbin/nimble_linux
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
        mv rootfs/usr/sbin/atbm_cli_6062 rootfs/usr/sbin/atbm_cli
        mv rootfs/usr/sbin/atbm_tool_6062 rootfs/usr/sbin/atbm_tool
        mv rootfs/usr/sbin/nimble_linux_6062 rootfs/usr/sbin/nimble_linux
    elif test $WIFI_TYPE = "atbm6062s" ; then
        cp scripts/wifi/atbm6062s.sh rootfs/usr/sbin/wifi_driver.sh
        cp scripts/wifi/wifi_bt_driver_6062s.sh rootfs/usr/sbin/
        cp scripts/wifi/connect_ap.sh rootfs/usr/sbin/
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli
        rm rootfs/usr/sbin/atbm_tool
        rm rootfs/usr/sbin/nimble_linux
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
        mv rootfs/usr/sbin/atbm_cli_6062 rootfs/usr/sbin/atbm_cli
        mv rootfs/usr/sbin/atbm_tool_6062 rootfs/usr/sbin/atbm_tool
        mv rootfs/usr/sbin/nimble_linux_6062 rootfs/usr/sbin/nimble_linux
    elif test $WIFI_TYPE = "ssv6x5x" ; then
        cp scripts/wifi/ssv6x5x.sh rootfs/usr/sbin/wifi_driver.sh
        cp -rf ./wifi/* rootfs/
        rm rootfs/usr/sbin/atbm_cli_6062
        rm rootfs/usr/sbin/atbm_tool_6062
        rm rootfs/usr/sbin/nimble_linux_6062
        rm rootfs/usr/sbin/atbm_cli_6132
        rm rootfs/usr/sbin/atbm_tool_6132
        rm rootfs/usr/sbin/nimble_linux_6132
        rm rootfs/usr/sbin/atbm_cli_6031x_300w
        rm rootfs/usr/sbin/atbm_tool_6031x_300w
        rm rootfs/usr/sbin/nimble_linux_6031x_300w
    else
        echo "Wifi Type Error, please check!"
        exit 1
    fi
}

process_fs_scripts()
{
    cp -rf scripts/fs/* rootfs/usr/sbin/
}

process_other_scripts()
{
    cp -rf scripts/others/* rootfs/usr/sbin/
    rm -rf rootfs/usr/sbin/key_gpio.sh
}

process_utils() #复制utils目录下的所有文件到rootfs目录中
{
	UTILS_SUPPORT=1

    if test $UTILS_SUPPORT -eq 1 ; then
        cp -rf utils/* rootfs/
        if 	test $CHIP_SERIES = "AK3918AV100" ; then
          rm rootfs/usr/sbin/dt320_param.bin
          rm rootfs/usr/sbin/dt480_param.bin
          rm rootfs/usr/sbin/dt640_param.bin
          rm rootfs/usr/sbin/fr224_param.bin
          rm rootfs/usr/sbin/fas224_param.bin
          rm rootfs/usr/sbin/model_0x0A000000_V1.1.01.bin
          rm rootfs/usr/sbin/model_0x0A000002_V1.1.02.bin
          rm rootfs/usr/sbin/model_0x0A00000D_V1.0.00.bin
          rm rootfs/usr/sbin/mobilenetv1_crydet_acc96_v1.bin
          rm rootfs/usr/sbin/model_0x0A00000A_V1.0.01.bin
          rm rootfs/usr/sbin/model_0x0A00000B_V1.0.00.bin
          rm rootfs/usr/sbin/model_0x0A00000C_V1.0.0.bin
          rm rootfs/usr/sbin/model_0x0A00000C_V1.0.00.bin
          rm rootfs/usr/sbin/model_0x0A000009_V1.0.00.bin
        elif 	test $CHIP_SERIES = "AK3918AV200" ; then
          rm rootfs/usr/sbin/yolov5n_640*
          #rm rootfs/usr/sbin/yolov5n_v1_640
          rm utils/usr/sbin/yolov5c_480*
          rm rootfs/usr/sbin/yolov5c_480*
          rm utils/usr/sbin/ak_ddr_param
          rm rootfs/usr/sbin/ak_ddr_param
        fi
    fi
}

#安装samples
install_samples() #安装示例文件
{
    if [ -d $sample_install_dir ];then
        cp -rf $sample_install_dir/* rootfs/usr/bin  #复制示例文件
    fi
}

# 安装各类脚本，所谓的安装脚本，只是把脚本放到了对应的位置
install_scripts()
{
    process_flash_scripts
    
    process_lcd_scripts

    process_screen_scripts

    process_sensor_scripts

    process_wifi_scripts

    process_fs_scripts

    process_other_scripts

    process_utils
}

prepare_image()
{
    extract_fs

    install_platform

    install_driver

    install_scripts

    install_samples
}

# 制作烧录镜像
make_image()
{
    # 根据选择的Flash类型制作烧录镜像
    if test $FLASH_TYPE = "NOR" ; then
        ./create_jffs2fs.sh #操作的目录是./usr/config目录，
        ./create_squashfs.sh #jffs2fs和squashfs是两种不同的文件系统，操作的目录是./usr/目录

        # 将制作好的烧录镜像放到burntool下
        #cp -rf usr.sqsh4 $image_install_dir
        cp -rf hongshi.jffs2 $image_install_dir
        cp -rf usr.jffs2 $image_install_dir
        cp -rf root.sqsh4 $image_install_dir
    elif test $FLASH_TYPE = "NAND" ; then
        ./create_yaffs2fs.sh
        cp -rf *.yaffs2 $image_install_dir 
        #先执行当前目录下的 create_yaffs2fs.sh 脚本，该脚本可能用于创建适用于 NAND Flash 的 YAFFS2 文件系统。
        #然后使用 cp -rf 命令将当前目录下所有扩展名为 .yaffs2 的文件或目录复制到 $image_install_dir 目录中
    fi

}

clean_image() #要清理文件系统镜像，但未执行任何实际清理操作
{
    echo "clean fs image..."
}

get_config_info #调用并执行get_config_info 函数

cd $rootfs_dir #进入根文件系统目录（由 $rootfs_dir 变量指定），后续操作将在此目录下进行

case "$1" in
	all)
		prepare_image
		make_image
		;; #all执行完整构建流程，调用 prepare_image 准备环境，再调用 make_image 生成镜像
	clean)
		clean_image
		;;
	*)
		echo "Usage:"
		echo " $0 all  : build all"
		echo " $0 clean: clean all"
		;;
esac

cd - #回到执行脚本前的目录，保持工作目录不变

